package net.mihome.app.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import net.mihome.app.R;
import net.mihome.app.bean.RssResource;

import java.util.List;

public class HListviewAdapter extends BaseAdapter {

    List<RssResource> mItems;
    LayoutInflater mInflater;
    private int selectedPosition = 0;// 选中的位置
	int mResource;
	int mTextResId;
    Context context;
		public HListviewAdapter(Context context, int resourceId, int textViewResourceId, List<RssResource> objects) {
			mInflater = LayoutInflater.from( context );
			mResource = resourceId;
			mTextResId = textViewResourceId;
			mItems = objects;
            this.context=context;
		}
		
		@Override
		public boolean hasStableIds() {
			return true;
		}

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public Object getItem(int i) {
        return mItems.get(i);
    }

    @Override
		public long getItemId( int position ) {
			return getItem( position ).hashCode();
		}
		
		@Override
		public int getViewTypeCount() {
			return 3;
		}
		
		@Override
		public int getItemViewType( int position ) {
			return position%3;
		}
		
		@Override
		public View getView( int position, View convertView, ViewGroup parent ) {
			if( null == convertView ) {
				convertView = mInflater.inflate( mResource, parent, false );
			}

			RssResource rssResource= (RssResource) getItem(position);
			TextView textView = (TextView) convertView.findViewById( mTextResId );
			textView.setText( rssResource.getTitle() );
            if (selectedPosition == position) {
                convertView.setSelected(true);
                convertView.setBackgroundResource(R.drawable.button_logout_bg);
                textView.setTextColor(Color.WHITE);
            } else {
                convertView.setSelected(false);
                convertView.setBackgroundColor(context.getResources().getColor(R.color.white));
                textView.setTextColor(context.getResources().getColor(R.color.grayslate));

            }
            int type = getItemViewType( position );

			return convertView;
		}

    public int getSelectedPosition() {
        return selectedPosition;
    }

    public void setSelectedPosition(int selectedPosition) {
        this.selectedPosition = selectedPosition;
    }
}